package com.duan.boot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.duan.boot.common.R;
import com.duan.boot.entity.Task;
import com.duan.boot.service.TaskPlusService;
import com.duan.boot.service.TaskService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;


@RestController
@RequestMapping("/v1/oa/task")
public class TaskController {
    private final TaskService taskService;

    private final TaskPlusService taskPlusService;

    @Autowired
    public TaskController(TaskService taskService,TaskPlusService taskPlusService) {
        this.taskService = taskService;
        this.taskPlusService=taskPlusService;
    }



    @ApiOperation(value = "增加任务")
    @PostMapping("/add")
    public R addOaTask(@RequestBody(required = true) Task task) {
        Integer i = taskService.insertOaTask(task);
        return R.ok(i>0 ? "success" :"failed");
    }

    @ApiOperation(value = "删除任务")
    @DeleteMapping("/delete/{id}")
    public R removeById(@PathVariable("id") Long id) {

        boolean remove = taskPlusService.removeById(id);
        return R.ok(remove ? "success" : "error");
    }

    @ApiOperation(value = "修改任务")
    @PutMapping("/save")
    public R updateOaTask(@RequestBody(required = true) Task task) {
        return R.ok(taskPlusService.saveOrUpdate(task));
    }


    @ApiOperation(value = "查找所有任务")
    @GetMapping("/findAll")
    public R getTaskList() {
        taskService.updateTaskState();
        return R.ok(taskService.getTaskList());
    }

    @ApiOperation(value = "通过任务id查找任务")
    @GetMapping("/getById/{id}")
    public R getById(@PathVariable("id") Long id) {
        return taskService.getById(id)==null ? R.fail("不存在这个任务"):R.ok(taskService.getById(id));
    }

    @ApiOperation(value = "通过任务名称查找任务")
    @GetMapping("/getByName/{name}")
    public R getByName(@PathVariable("name") String name){
        return taskService.getByName(name)==null ? R.fail("不存在这个任务"):R.ok(taskService.getByName(name));
    }

    @ApiOperation("根据id或者name或者状态直接查找任务")
    @RequestMapping(value = "/getByIdOrNameOrState", method = RequestMethod.GET)
    public R getByIdOrName(@RequestParam Object idOrNameOrState){
        return taskService.getByIdOrNameOrState(idOrNameOrState)==null ?
                R.fail("不存在这个任务") : R.ok(taskService.getByIdOrNameOrState(idOrNameOrState));
    }


    @ApiOperation(value = "通过任务状态查找任务")
    @GetMapping("/getByState/{state}")
    public R<Object> getByStatus(@PathVariable("state") Integer state) {
        List<Task> task = taskService.getByState(state);
        return R.ok(task);
    }

    @ApiOperation("查询任务个数")
    @RequestMapping(value = "/numberOfTask", method = RequestMethod.GET)
    public R numberOfDep(){
        return R.ok(taskService.getNumberOfTask());
    }

    @ApiOperation("分页查询")
    @RequestMapping(value = "/pagedQuery", method = RequestMethod.GET)
    public R pagedQuery(@RequestParam Integer page,
                        @RequestParam Integer numberOfPage){
        return R.ok(taskService.pagedQuery(page,numberOfPage));
    }

    @ApiOperation("分页查询页码个数")
    @RequestMapping(value = "/pagedQueryPage", method = RequestMethod.GET)
    public R pagedQueryPage(@RequestParam Integer numberOfPage){
        return R.ok(taskService.pagedQueryPage(numberOfPage));
    }

    @ApiOperation("更新任务状态")
    @RequestMapping(value = "/updateTaskState", method = RequestMethod.PUT)
    public Void updateTaskState(){
        taskService.updateTaskState();
        return null;
    }
}